package com.wjw.java.io.transformation;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 字符输入流：InputStreamReader解决读取txt中文乱码问题
 * 1.把FileInputStream字节流,转为字符流InputStreamReader
 * 2.指定字符编码
 * 3.将InputStreamReader传入到BufferedReader,提高读取效率
 */
public class InputStreamReader_ {

    public static void main(String[] args) {
        String filePath = "f:\\IO\\question_code2.txt";//读取ANSI格式的文本文件
        InputStreamReader isr = null;
        BufferedReader br = null;
        String line = "";
        try {
            //1.将FileInputStream转换为InputStreamReader,并指定字符编码,解决中文乱码
            isr = new InputStreamReader(new FileInputStream(filePath),"GBK");
            //2.将InputStreamReader传入到BufferReader,提高读取效率
            br = new BufferedReader(isr);
            //循环读取txt中的数据,并在控制台打印
            while ((line = br.readLine()) != null){
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //这里只需关闭BufferedReader,它的底层会去关闭传入的InputStreamReader
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
